//
//  LXTabBarController.m
//  SevenFM
//
//  Created by seven on 2018/4/24.
//  Copyright © 2018年 seven. All rights reserved.
//

#import "LXTabBarController.h"
#import "LXNavgationController.h"
#import "UIImage+LXImage.h"
#import "LXTabBar.h"
#import "LXMiddleView.h"

@interface LXTabBarController ()

@end

@implementation LXTabBarController

+ (instancetype)shareInstance {
    static LXTabBarController *tabBarVc;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tabBarVc = [[LXTabBarController alloc] init];
    });
    return tabBarVc;
}

+ (instancetype)tabBarControllerWithAddChildVcsBlock:(void (^)(LXTabBarController *))addVcBlock {
    LXTabBarController *tabbarVc = [[LXTabBarController alloc] init];
    if (addVcBlock) {
        addVcBlock(tabbarVc);
    }
    return tabbarVc;
}

- (void)addChildVC:(UIViewController *)vc normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName isRequiredNavController:(BOOL)isRequired {
    if (isRequired) {
        LXNavgationController *nav = [[LXNavgationController alloc] initWithRootViewController:vc];
        nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:[UIImage originImageWithName:normalImageName] selectedImage:[UIImage originImageWithName:selectedImageName]];

        [self addChildViewController:nav];

    } else {
        [self addChildViewController:vc];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTabbar];
}

- (void)setupTabbar {
    [self setValue:[[LXTabBar alloc] init] forKey:@"tabBar"];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    [super setSelectedIndex:selectedIndex];
    UIViewController *vc = self.childViewControllers[selectedIndex];
    if (vc.view.tag == 666) {
        vc.view.tag = 888;

        LXMiddleView *middleView = [[LXMiddleView alloc] init];
        middleView.middleClcikBlock = [LXMiddleView shareInstance].middleClcikBlock;
        middleView.isPlaying = [LXMiddleView shareInstance].isPlaying;
        middleView.middleImg = [LXMiddleView shareInstance].middleImg;
        CGRect frame = middleView.frame;
        frame.size.width = 65;
        frame.size.height = 65;
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        frame.origin.x = (screenSize.width - 65) * 0.5;
        frame.origin.y = screenSize.height - 65;
        middleView.frame = frame;
        [vc.view addSubview:middleView];
    }
}

@end
